package com.tcreditdemo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tcredit.sdk.android.WebActivity;

import java.util.ArrayList;

public class HomeRecyclerviewAdapter extends RecyclerView.Adapter<HomeRecyclerviewAdapter.HomeHolder> {

    public Context context;
    ArrayList<ArrayList<Integer>> datas;
    public boolean state;

    public HomeRecyclerviewAdapter(Context context, ArrayList<ArrayList<Integer>> datas,boolean state) {
        this.context = context;
        this.datas = datas;
        this.state =state;
    }

    @Override
    public HomeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_adapter_view, parent, false);
        return new HomeHolder(view);
    }

    @Override
    public void onBindViewHolder(HomeHolder holder, int position) {
        LogUtil.e("当前位置是bindholder："+position);
        //线性布局 (已经把布局里面的分割线隐藏)
        if (state) {
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            holder.cardrecycler.setLayoutManager(layoutManager);
        }else {

            //todo 这个是区分标题和 recyclerview 中的内容，如果不知道标题在哪里的可以注释掉下面的内容再打开
            GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
            layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (position == 0) {
                        return 2;
                    } else {
                        return 1;
                    }
                }
            });
            holder.cardrecycler.setLayoutManager(layoutManager);
        }

        // 如果是嵌套的recyclerview的话这里就需要进行数据的拆分
        final CardRecyclerAdapter adapter = new CardRecyclerAdapter(datas.get(position),holder.cardrecycler);
        holder.cardrecycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new CardRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String title) {
                ToastUtil.showToastCenter(title);
//                if (title.equals("邮箱账单")) {
//                    context.startActivity(new Intent(context, WebActivity.class));
//                }
            }
        });


    }



    @Override
    public int getItemCount() {
        return datas.size();
    }


    class HomeHolder extends RecyclerView.ViewHolder{
        public CardView cardView;
        public RecyclerView cardrecycler;

        public HomeHolder(View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.cardview);
            cardrecycler = itemView.findViewById(R.id.card_recycler);
        }
    }



}



